package com.crm.system.controller.productManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.productManager.CustomerMemberProductEditQO;
import com.crm.model.qo.productManager.ProductQRCodeQO;
import com.crm.model.vo.productManager.CustomerProductDetailVO;
import com.crm.system.service.productManager.CustomerMemberProductService;
import com.crm.system.service.productManager.CustomerProductService;
import com.google.gson.Gson;
import com.sun.javafx.collections.MappingChange;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanyunfeng
 * @date 2021/4/12 15:10
 * 产品管理详情
 */

@RestController
@RequestMapping("/customerProduct/detail")
@Slf4j
public class CustomerMemberProductDetailController {

    @Autowired
    private CustomerMemberProductService customerMemberProductService;


    /**
     * 根据产品id查询产品详情
     *
     * @param memberProductId
     * @return
     */
    @GetMapping("/info")
    public DataResponse queryCustomerProductDetailInfo(@RequestParam(value = "memberProductId") String memberProductId) {
//        log.info("开始调用查询产品明细的方法,接收到的产品id是【{}】", memberProductId);
        List<CustomerProductDetailVO> produsctDetai = customerMemberProductService.queryCustomerProductDetailInfo(memberProductId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), produsctDetai);
    }


    /**
     * 生成产品的二维码
     *
     * @param ProductQRCodeQO
     * @return
     */
    @PostMapping("/createDRcode")
    public DataResponse createDRCode(@RequestBody ProductQRCodeQO createQRCodeReq) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        createQRCodeReq.setUserId(userAccount.getUserId());//当前用户id
        log.info("开始调用生成二维码的方法，接收到的参数是【{}】", new Gson().toJson(createQRCodeReq));
        Map<String, String> map = new HashMap<String, String>();
        String base64 = customerMemberProductService.createDRCode(createQRCodeReq);
        map.put("base64", base64);
        map.put("link", "http://www.baidu.com");
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), map);
    }


    /**
     * 根据token跳转到产品链接
     *
     * @param token
     * @return
     */
    @GetMapping("/link")
    public DataResponse copyLink(@RequestParam("token") String token) {
        String result = customerMemberProductService.copyLink(token);

        if (StringUtils.isNotBlank(result)) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), result);
        }

        return new DhResponse(ResponseCodeEnum.TOKEN_INVALID.getResponseCode());

    }


    /**
     * 修改卡片价格
     * @param memberEditQO 会员产品列表--服务向管理 编辑实体
     * @return
     */
    @PostMapping("/updateCardPrice")
    public DataResponse updateCardPrice(@RequestBody CustomerMemberProductEditQO memberEditQO) {
        if (!customerMemberProductService.updateCardPrice(memberEditQO)) {
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());

    }


}
